package com.tnovoselec.android.travelassistant.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.dto.GasPriceDto;

public class GasPricesAdapter extends BaseAdapter {

	private List<GasPriceDto> prices;
	private LayoutInflater inflater;
	private GasPriceDto tempGasPrice;
	private Context context;

	public GasPricesAdapter(Context c) {
		context = c;
		inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {

		return prices != null?prices.size():0;
	}

	@Override
	public Object getItem(int position) {
		return prices.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		ViewHolder holder;
		tempGasPrice = prices.get(position);
		if (convertView == null) {
			v = inflater.inflate(R.layout.gas_prices_item, parent, false);
			holder = new ViewHolder();
			holder.image = (ImageView) v.findViewById(R.id.gas_price_image);
			holder.name = (TextView) v.findViewById(R.id.gas_price_name);
			holder.price = (TextView) v.findViewById(R.id.gas_price_price);
			v.setTag(holder);
		} else {
			v = convertView;
		}
		holder = (ViewHolder) v.getTag();
		holder.image.setBackgroundResource(R.drawable.gas_station_resized);
		holder.name.setText(tempGasPrice.getName());
		holder.price.setText(tempGasPrice.getPrice() + " " + context.getString(R.string.currency));
		
		return v;
	}
	
	public void setPrices(List<GasPriceDto> prices){
		this.prices = prices;
		notifyDataSetChanged();
	}

	class ViewHolder {
		ImageView image;
		TextView name;
		TextView price;
	}
}
